package com.feihong.txgw.tx_precenter;

import com.alibaba.fastjson.JSON;
import com.feihong.txgw.inter.contract.recharge.RechargeContract;
import com.feihong.txgw.json.recharge.PhoneLocationInfo;
import com.feihong.txgw.json.recharge.SubmitOrderInfo;
import com.feihong.txgw.tx_model.RechargeMainModel;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName RechargeMainPrecenter
 * @package com.feihong.txgw.tx_precenter
 * @data 2018/5/7 11:30
 * @Describe
 * @Change
 */
public class RechargeMainPrecenter extends RechargeContract.AbRechargeMainPrecenter {

    public static RechargeMainPrecenter create(){
        return new RechargeMainPrecenter();
    }
    @Override
    public RechargeContract.IRechargeMainModel getModel() {
        return RechargeMainModel.create();
    }

    @Override
    public void onStart() {

    }

    @Override
    public void loadOperator(String rechargeType, String rechargeTel) {
        mIModel.getOperators(rechargeType,rechargeTel).subscribe(new Observer<PhoneLocationInfo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(PhoneLocationInfo o) {
                mIView.loadOperatorsResult(o);
            }

            @Override
            public void onError(Throwable e) {
                mIView.loadDataError();
            }

            @Override
            public void onComplete() {
                mIView.loadDataComplated();
            }
        });
    }

    @Override
    public void submitRechargeOrder(String rechargeType, String rechargeTel, String terminalType, String goodsID) {
        mIModel.submitRechargeOrder(rechargeType,rechargeTel,terminalType,goodsID).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(String result) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                    if (!"200".equals(jsonObject.optString("code"))) {
                        mIView.dismissProgressDialog();
                        mIView.showDialog("提交订单失败", "原因：\n" + jsonObject.optString("msg"));
                    }else {
                        SubmitOrderInfo orderInfo = JSON.parseObject(jsonObject.optJSONObject("data").toString(), SubmitOrderInfo.class);
                        mIView.submitRechargeOrderResult(orderInfo);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    mIView.showDialog(null,"提交订单失败");
                }
            }

            @Override
            public void onError(Throwable e) {
                mIView.loadDataError();
            }

            @Override
            public void onComplete() {
                mIView.loadDataComplated();
            }
        });
    }
}
